import java.util.Arrays;

public class Test {
    //拷贝函数
    public static void main(String[] args) {
        int[] array = {1,2,3,4};
        int[] ret = new int[array.length * 2];
        //ret = Arrays.copyOf(array,array.length * 2);  //可以当作扩容来看待
        //System.out.println(Arrays.toString(ret));

        //也可以直接调用Arrays.copyOf方法内部中调用的函数System.arraycopy
        System.arraycopy(array,0,ret,1,array.length);
        System.out.println(Arrays.toString(ret));
    }

    //改进版
    public static void main7(String[] args) {
        int[] array = {1,2,3,4};
        String array2 = myToString2(array);
        System.out.println(array2);
    }
    public static String myToString2(int[] tmpArr) {
        //此时注意判断一下传过来的数组是否是空数组
        if(tmpArr == null) {
            return "null";
        }
        String ret = "[";
        for (int i = 0; i < tmpArr.length; i++) {
            ret += tmpArr[i];
            if(i != tmpArr.length - 1) {
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }

    //练习：把数组中的内容以字符串的形式打印出来
    public static void main6(String[] args) {
        int[] array = {1,2,3,4};
        myToString(array);
    }
    public static void myToString(int[] tmpArr) {
        String ret = "";
        System.out.print("[");   //注意这个方法不好，一般方法功能比较单一，一般不加打印功能
        for (int i = 0; i < tmpArr.length; i++) {
            //System.out.print(tmpArr[i]);
            if(i != tmpArr.length - 1) {
                System.out.print(tmpArr[i] + ",");
            }
            if(i == tmpArr.length - 1) {
                System.out.print(tmpArr[i]);
            }

        }
        System.out.println("]");
    }

    //数组进行传参，写一个数组，并扩大2倍数组里面的值
    public static void main5(String[] args) {
        int[] array = {1,2,3,4,5,6};
        grow(array);
        System.out.println(Arrays.toString(array));
    }

    public static void grow(int [] tmpArr) {
        for (int i = 0; i < tmpArr.length; i++) {
            tmpArr[i] *= 2;
        }
    }

    public static void main4(String[] args) {
        int a = 10;
        a = func(a);
        System.out.println(a);
    }
    public static int func(int b) {
        b = 20;
        return b;
    }

    //数组传参
    public static void main3(String[] args) {
        int[] array1 = {1,2,3,4};
        array1[0] = 99;

        int[] array2 = array1;
        array2[0] = 100;
        System.out.println(Arrays.toString(array1));  //分别输出什么:[100 2 3 4]
        System.out.println(Arrays.toString(array2));  //【00 2 3 4】
    }

    //空指针异常情况
    public static void main2(String[] args) {
        int[] array = null;
        System.out.println(array);
    }


    //创建一个数组，打印出1-100的数字
    public static void main1(String[] args) {
        int[] array = new int[100]; //java中不赋出值，默认都是0
        for (int i = 0; i < array.length; i++) {
            array[i] = i + 1;
        }
        printArray(array);
        //System.out.println(Arrays.toString(array));
    }

    public static void printArray(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = i + 1;
            System.out.println(array[i]+" ");
        }
        //System.out.println();
    }
}
